<?php

class Admin_AuthController extends Zend_Controller_Action
{
	public function indexAction()
	{
		$form = new SON_Forms_Login();
		$this->view->form = $form;
	
		if($this->_request->isPost()) {
			$data = $this->_request->getPost();
			
			if($form->isValid($data)) {
				$authAdapter = $this->getAuthAdapter();
				$authAdapter->setIdentity($data["email"])
							->setCredential($data['senha']);
		
				$result = $authAdapter->authenticate();
				if($result->isValid()) {
					$auth = Zend_Auth::getInstance();
					$auth->setStorage(new Zend_Auth_Storage_Session("admin"));
					$dataAuth = $authAdapter->getResultRowObject(null, 'senha');
					$dataAuth->role = 'admin';
					$auth->getStorage()->write($dataAuth);
					$this->_redirect("admin");
				} else {
					$this->view->error = "Usuário/Senha Inválidos";
				}
			}
		}
	}
	
	public function logoutAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$auth = Zend_Auth::getInstance();
		$auth->setStorage(new Zend_Auth_Storage_Session("admin"));
		$auth->clearIdentity();
		$this->_redirect("admin/auth");
	}
	
	private function getAuthAdapter()
	{
		$bootstrap = $this->getInvokeArg("bootstrap");
		$resource = $bootstrap->getPluginResource("db");
	
		$db = $resource->getDbAdapter();
	
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		$authAdapter->setTableName("admin")
					->setIdentityColumn("email")
					->setCredentialColumn("senha")
					->setCredentialTreatment('SHA1(?)');
	
		return $authAdapter;
	}
}

